package vn.fpt.snake.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.KeyEvent;

import vn.fpt.snake.commons.Constants;
import vn.fpt.snake.controller.GameControllerImpl;
import vn.fpt.snake.controller.OptionsControllerImpl;
import vn.fpt.snake.dataAccess.DataAccess;
import vn.fpt.snake.entity.HighScore;
import vn.fpt.snake.entity.Settings;
import vn.fpt.snake.view.Board;

public class HighScoreViewImpl implements IView {
	Board parent;
	OptionsControllerImpl optionController;
	GameControllerImpl gameController;
	DataAccess dataAccess;
	List<HighScore> highScores;
	Settings settings;

	public HighScoreViewImpl(Board parent) throws Exception {
		this.parent = parent;
		optionController = new OptionsControllerImpl();
		gameController = new GameControllerImpl();
		highScores = new ArrayList<>();
		highScores = optionController.getHighScores();
		dataAccess = new DataAccess();
		settings = dataAccess.getSettings();
	}

	@Override
	public void onPaint(Graphics g) throws Exception {
		// TODO Auto-generated method stub
		int size = highScores.size();

		int width = parent.getWidth();
		int height = parent.getHeight();
		int dotSize = settings.getDotSize();
		Font font = new Font("Cooper Black", Font.BOLD, (int)(dotSize * 1.5));
		FontMetrics metrics = g.getFontMetrics(font);	
		String highScoreText = Constants.HIGH_SCORES;
		
		int highScoreWidth = metrics.stringWidth(highScoreText);
		int heightOfFont = metrics.getHeight();
		int xHighScore = (width - highScoreWidth) / 2;
		int yHighScore = (height / 3) - heightOfFont;
		
		g.drawImage(gameController.getImage(Constants.GAME_OVER_IMG), 0, 0, width, height, parent);
		g.setColor(Color.WHITE);
		g.drawString(Constants.BACK_INSTRUCTION, dotSize, dotSize);
		g.setFont(font);
		g.setColor(new Color(213, 124, 54));
		g.drawString(highScoreText, xHighScore, yHighScore);
		
		
		for(int i = 0; i < size; i++){
			HighScore highScore = highScores.get(i);
			String indexNumber = i == 0 ? Constants.FIRST_POSTION : i == 1 ? Constants.SECOND_POSITION : Constants.THIRD_POSITION;
			String highScoreRecord = String.format(indexNumber, highScore.getName(), highScore.getScore());
			
			font = new Font("Cooper Black", Font.BOLD, dotSize);
			metrics = g.getFontMetrics(font);
			
			int xName = width / 3;
			int yName = yHighScore + heightOfFont + metrics.getHeight() * (i + 2);
			if(i == 0){
				g.setColor(Color.RED);
			}else{
				g.setColor(Color.BLUE);
			}
			g.drawString(highScoreRecord, xName, yName);
		
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		try{
			int keyCode = e.getKeyCode();
			if(parent._view instanceof HighScoreViewImpl){
				if(keyCode == KeyEvent.VK_ESCAPE){					
					gameController.switchStartupView(parent);
				}
			}
		}catch (Exception ex) {
			// TODO: handle exception
			ex.printStackTrace();
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

}
